# 制 作 人：祝金宝
# 制作时间：2022/7/11 10:02
# 制作用途：仅学习（不负任何责任）
import urllib.request
import os

url = 'http://nginx.org/download/'


class NginxInstallMake:
    def __init__(self):
        self.url = url
        self.download()
        self.install()
        self.check()

    @staticmethod
    def download():
        global filename, info
        with open(os.path.dirname(os.path.dirname(os.path.realpath(__file__))) + '/info.conf', "r", encoding="utf-8") as f:
            info = f.read().split()
        filename = 'nginx-' + info[1].split("=")[-1] + '.tar.gz'
        try:
            urllib.request.urlretrieve(url + filename, filename)
        except:
            print("版本号不对")

    @staticmethod
    def install():
        sh = """yum -y install gcc gcc-c++ autoconf automake make \n
        yum install -y pcre pcre-devel
        yum install -y zlib*
        """+"tar -xf " + filename + """\n
        cd """ + 'nginx-' + info[1].split("=")[-1] + """\n
        ./configure""" + """\n
        make && make install""" + """
        /usr/local/nginx/sbin/nginx"""
        with open("install.sh", "w") as f:
            f.write(sh)
        os.system("bash install.sh")

    def check(self):
        print("##################验证端口与进程#######################")
        for line in ["ps -ef|grep nginx|egrep -v grep", "ss -nutlp|grep nginx"]:
            os.system(line)
            print("####################################################")

if __name__ == '__main__':
    NginxInstallMake()